package com._520it.crm.web.controller;

import com._520it.crm.domain.Employee;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.EmployeeQueryObject;
import com._520it.crm.service.IEmployeeService;
import com._520it.crm.util.AjaxResult;
import com._520it.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by ASUS on 2017/10/9.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("")
    @RequiresPermissions("employee:index")
    public String index(){
        return "employee";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("employee:list")
    @PermissionName("员工加载")
    public PageResult list(EmployeeQueryObject qo){

        return employeeService.query(qo);

    }

    @RequestMapping("/save")
    @ResponseBody
    @RequiresPermissions("employee:save")
    @PermissionName("员工添加")
    public AjaxResult save(Employee employee){
        AjaxResult result = null;
        try{
            employeeService.insert(employee);
            result = new AjaxResult("保存成功");
        }catch (Exception e){
            result = new AjaxResult(false,"保存失败");
        }

        return result;
    }
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("employee:update")
    @PermissionName("员工更新")
    public AjaxResult update(Employee employee){
        AjaxResult result = null;
        try{

            employeeService.updateByPrimaryKey(employee);
            result = new AjaxResult("修改成功");
        }catch (Exception e){
            result = new AjaxResult(false,"修改失败");
        }

        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions("employee:delete")
    @PermissionName("员工删除")
    public AjaxResult delete(Long id){
        AjaxResult result = null;
        try{
            employeeService.deleteByPrimaryKey(id);
            result = new AjaxResult("删除成功");
        }catch (Exception e){
            result = new AjaxResult(false,"删除失败");
        }
        return result;
    }
    @RequestMapping("/selectListForEmployeeForm")
    @ResponseBody
    public List<Employee>  selectListForEmployeeForm(){
        return employeeService.selectAll();
    }

    @RequestMapping("/selectUser")
    @ResponseBody
    public List<Employee> selectUser( ){
        return employeeService.selectAll();
    }
}
